package timer;

import java.util.Calendar;
import java.util.Timer;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServlet;

public class TimerServlet extends HttpServlet implements ServletContextListener
{
    private Timer timer = null;

    private Calendar cal = Calendar.getInstance();
    
    private int timeSpace = 20;  //时间间隔 s
    Task task = new TaskImpl();

    public void contextInitialized(ServletContextEvent arg0)
    {
        timer = new java.util.Timer(true);
      //  System.out.println(cal.getTime() + "Timer start...");
        timer.schedule(new OOS(), 0, 1000 * timeSpace);
      //  System.out.println(cal.getTime() + "Task ready...");
    }

    public void contextDestroyed(ServletContextEvent arg0)
    {
        timer.cancel();
        System.out.println(cal.getTime() + "Timer destoried!");
    }

}
